//
//  ViewController.m
//  03-17-小人快跑
//
//  Created by student on 16/3/17.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "MyImageView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MyImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableArray *mArr=[NSMutableArray array];
    for (int i=1; i<=2; i++) {
        UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"chara%i",i]];
        [mArr addObject:image];
    }
    
    //小人图片切换的图片数组
    self.imageView.animationImages=mArr;
    //图片切换周期
    self.imageView.animationDuration=0.5;
    
    
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.imageView stopAnimating];//小人停止闪烁
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.imageView startAnimating];//小人闪烁
    UITouch *touch=touches.anyObject;
    //得到触摸点
    CGPoint point=[touch locationInView:self.view];
    
    //移动周期
    int time=MAX(fabs(fabs(point.x)-fabs(self.imageView.center.x)), fabs(fabs(point.y)-fabs(self.imageView.center.y)))/8;
    
    //隐式动画
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:time];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationRepeatAutoreverses:NO];
    
    // 中止值
    self.imageView.center = point;
    [UIView commitAnimations];
}



@end
